#include "io.h"

#ifdef USE_API_IO_WIN32

#include <windows.h>
#include <graphics.h>

void _io_init(void) {}
void _io_exit(void) {}

static int _io_player_keys[PLAYER_MAX] = {0, 0, 0, 0};

int io_player_get_buttons(int player)
{
    return _io_player_keys[player];
}

void io_player_read_buttons(void)
{
    _io_player_keys[0] = _io_player_keys[1] = _io_player_keys[2] = _io_player_keys[3] = 0;


    // JOUEUR 1
    if(GetAsyncKeyState(VK_LEFT)<-1)    _io_player_keys[0] |= IO_PLAYER_LEFT;
    if(GetAsyncKeyState(VK_RIGHT)<-1)   _io_player_keys[0] |= IO_PLAYER_RIGHT;
    if(GetAsyncKeyState(VK_UP)<-1)      _io_player_keys[0] |= IO_PLAYER_TOP;
    if(GetAsyncKeyState(VK_DOWN)<-1)    _io_player_keys[0] |= IO_PLAYER_BOTTOM;
    if(GetAsyncKeyState(VK_BACK)<-1)    _io_player_keys[0] |= IO_PLAYER_CIRCLE;
    if(GetAsyncKeyState(VK_RETURN)<-1)  _io_player_keys[0] |= IO_PLAYER_CROSS;
    if(GetAsyncKeyState(VK_ESCAPE)<-1)  _io_player_keys[0] |= IO_PLAYER_PAUSE;
    // JOUEUR 2
    if(GetAsyncKeyState(0x51)<-1)       _io_player_keys[1] |= IO_PLAYER_LEFT;
    if(GetAsyncKeyState(0x44)<-1)       _io_player_keys[1] |= IO_PLAYER_RIGHT;
    // JOUEUR 3
    if(GetAsyncKeyState(0x59)<-1)       _io_player_keys[2] |= IO_PLAYER_LEFT;
    if(GetAsyncKeyState(0x49)<-1)       _io_player_keys[2] |= IO_PLAYER_RIGHT;
    // JOUEUR 4
    if(GetAsyncKeyState(0x64)<-1)       _io_player_keys[3] |= IO_PLAYER_LEFT;
    if(GetAsyncKeyState(0x66)<-1)       _io_player_keys[3] |= IO_PLAYER_RIGHT;

    //return;
}

void io_get_mouse(float *x, float *y, int *buttons_state)
{
    // Read mouse coordinates and interpolate
    GFX_Resolution res;
    gfx_get_resolution(&res);
    *x = mousex() / (res.width / 640.);
    *y = mousey() / (res.height / 360.);

    // Read mouse buttons state
    *buttons_state = 0;

    if(GetAsyncKeyState(VK_LBUTTON))
    {
        *buttons_state |= IO_MOUSE_LBUTTON;
    }
    if(GetAsyncKeyState(VK_RBUTTON))
    {
        *buttons_state |= IO_MOUSE_RBUTTON;
    }
    return;
}

#endif
